package com.luck.emp.cg.logic;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.luck.emp.cg.qry.CgQry;
import com.luck.emp.common.SpOrderNoTwo;
import com.luck.emp.entity.EmpTCgD;
import com.luck.emp.entity.EmpTCgM;
import com.luck.emp.entity.EmpTProdBase;
import com.luck.pf.common.entity.UserBean;
import com.luck.pf.framework.dao.BaseJdbcDao;
import com.luck.pf.framework.logic.BaseLogic;
import com.luck.pf.util.CriteriaUtil;
import com.luck.pf.util.DateUtil;
import com.luck.pf.util.ToolUtil;

/**
 * 说明 Copyright: Copyright (c) 2011 深圳**科技
 * 
 * @author
 * 
 */
public class CgLogic extends BaseLogic implements ICgLogic {
	private BaseJdbcDao jdbcDao;

	/**
	 * 查找分页信息
	 */

	public <T> T findCgPage(CgQry qry, Class<T> cls) {
		Criteria criteria = baseDao.createCriteria(EmpTCgM.class);
		CriteriaUtil.addEq(criteria, "cgCode", qry.getCgCode());
		CriteriaUtil.addFullLike(criteria, "contact", qry.getContact());
		criteria.add(Restrictions.between("createDate",
				DateUtil.getStartDateDay(qry.getStartDate()),
				DateUtil.getEndDateDay(qry.getEndDate())));
		Order order = Order.desc("createDate");
		criteria.addOrder(order);
		if(qry.getOrgId() != null)
			CriteriaUtil.addEq(criteria, "deptId", qry.getOrgId());
		else{
			criteria.createAlias("dept", "d");
			filterData(criteria, qry.getUserInfo(), "d", null);
		}
		if (cls.getName().equals("java.util.List")) {
			return (T) criteria.list();
		} else {
			return (T) baseDao.pagedQuery(criteria, qry.getPageNo(),qry.getPageSize());
		}
	}

	/**
	 * 保存
	 **/
	public String saveCg(UserBean userBean, EmpTCgM entity,String[] productCodes, String[] priceReals, String[] qtys, Long[] ids) throws Exception {
		entity.setModiDate(DateUtil.getDate());
		entity.setModiUser(userBean.getUser());
		String cgCode = entity.getCgCode();
		if(StringUtils.isBlank(entity.getCgCode())){//新建
			entity.setCreateDate(DateUtil.getDate());
			entity.setCreateUser(userBean.getUser());
			SpOrderNoTwo sp = new SpOrderNoTwo(this.jdbcDao.getJdbcTemplate());
			cgCode = sp.execute(userBean.getDept().getDeptCode(), "CG");
			entity.setCgCode(cgCode);
			entity.setDeptId(userBean.getDeptId());
			entity.setStatus("1");
		}

		if(!entity.getStatus().equals("1"))
			throw new Exception("<br>此状态非新建，不能修改");
		
		super.save(entity);
		List<EmpTCgD> dd = findCgd(cgCode);
		if(productCodes != null){
			for(int i=0; i < productCodes.length; i ++){
				EmpTProdBase base = super.get(EmpTProdBase.class, productCodes[i]);
				EmpTCgD cgd = null;
				if(ids[i] != null){
					cgd = super.get(EmpTCgD.class, ids[i]);
					if(dd.contains(cgd))
						dd.remove(cgd);
				}
				if(cgd == null)
					cgd = new EmpTCgD();	
				cgd.setCgCode(cgCode);
				cgd.setProductCode(productCodes[i]);
				cgd.setConfirmIn(0L);
				cgd.setDiscRatio(1.00);
				cgd.setHxStatus("1");//未核销
				cgd.setMeasUnit(base.getMeasUnit());
				cgd.setPayAmt(0.00);
				cgd.setPriceReal(Double.parseDouble(priceReals[i]));
				cgd.setPurPrice(base.getSale().getPurPrice());
				cgd.setQty(Long.parseLong(qtys[i]));
				super.save(cgd);
			}
		}
		
		super.baseDao.getHibernateTemplate().deleteAll(dd);
		
		return null;
	}

	/**
	 * 删除
	 */
	public String deleteCg(UserBean userBean, EmpTCgM entity) throws Exception {
		if(!entity.getStatus().equals("1"))
			throw new Exception("<br>此状态非新建，不能删除");
		super.delete(entity);
		super.baseDao.getHibernateTemplate().deleteAll(findCgd(entity.getCgCode()));
		return null;
	}
	public List<EmpTCgD> findCgd(String cgCode){
		List<EmpTCgD> ls = super.baseDao.find(" FROM EmpTCgD WHERE cgCode=?",cgCode);
		return ls;
	}
	public BaseJdbcDao getJdbcDao() {
		return jdbcDao;
	}

	public void setJdbcDao(BaseJdbcDao jdbcDao) {
		this.jdbcDao = jdbcDao;
	}

}